package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.symmetricds.AppConfig;
import cn.ekuma.epos.symmetricds.bean.NodeGroupLink;
import cn.ekuma.epos.symmetricds.bean.Router;

public class RouterDAO extends BaseDAO<Router> {

	public RouterDAO(I_Session s) {
		super(s);
		
	}

	@Override
	public Router readValues(DataRead dr,Router obj) throws BasicException {
		if(obj==null)
			obj=new Router();
		obj.setRouterId(dr.getString(1));
		obj.setTargetCatalogName(dr.getString(2));
		obj.setTargetSchemaName(dr.getString(3));
		obj.setTargetTableName(dr.getString(4));
		NodeGroupLink group=new NodeGroupLink();
		group.setSourceNodeGroupId(dr.getString(5));
		group.setTargetNodeGroupId(dr.getString(6));
		obj.setNodeGroupLink(group);
		
		obj.setRouterType(dr.getString(7));
		obj.setRouterExpression(dr.getString(8));
		obj.setSyncOnUpdate(dr.getBoolean(9));
		obj.setSyncOnInsert(dr.getBoolean(10));
		
		obj.setSyncOnDelete(dr.getBoolean(11));
		obj.setCreateTime(dr.getTimestamp(12));
		obj.setLastUpdateBy(dr.getString(13));
		obj.setLastUpdateTime(dr.getTimestamp(14));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
				AppConfig.getAppProperty(AppConfig.TABLE_PREFIX)+"_"+"router",
                new Field[]{
                   new Field("router_id",Datas.STRING, Formats.STRING),
                   new Field("target_catalog_name",Datas.STRING, Formats.STRING),
                   new Field("target_schema_name",Datas.STRING, Formats.STRING),
                   new Field("target_table_name",Datas.STRING, Formats.STRING),
                   new Field("source_node_group_id",Datas.STRING, Formats.STRING),
                   
                   new Field("target_node_group_id",Datas.STRING, Formats.STRING),
                   new Field("router_type",Datas.STRING, Formats.STRING),
                   new Field("router_expression",Datas.STRING, Formats.STRING),
                   new Field("sync_on_update",Datas.BOOLEAN, Formats.BOOLEAN),
                   new Field("sync_on_insert",Datas.BOOLEAN, Formats.BOOLEAN),
                   
                   new Field("sync_on_delete",Datas.BOOLEAN, Formats.BOOLEAN),
                   new Field("create_time",Datas.TIMESTAMP, Formats.TIMESTAMP),
                   new Field("last_update_by",Datas.STRING, Formats.STRING),
                   new Field("last_update_time",Datas.TIMESTAMP, Formats.TIMESTAMP)
                 },
                new int[]{0}
                );
	}

	/**
	 * <table name="router" description="Configure a type of router from one node group to another.  Note that routers are mapped to triggers through trigger_routers.">
        <column name="router_id" type="VARCHAR" size="50" required="true" primaryKey="true"  description="Unique description of a specific router" />
        <column name="target_catalog_name" type="VARCHAR" size="50"  description="Optional name for the catalog a target table is in. Only use this if the target table is not in the default catalog." />
        <column name="target_schema_name" type="VARCHAR" size="50"  description="Optional name of the schema a target table is in. On use this if the target table is not in the default schema." />
        <column name="target_table_name" type="VARCHAR" size="50"  description="Optional name for a target table.  Only use this if the target table name is different than the source." />
        <column name="source_node_group_id" type="VARCHAR" size="50" required="true"  description="Routers with this node_group_id will install triggers that are mapped to this router." />
        <column name="target_node_group_id" type="VARCHAR" size="50" required="true"  description="The node_group_id for nodes to route data to.  Note that routing can be further narrowed down by the configured router_type and router_expression." />
        <column name="router_type" type="VARCHAR" size="50"  description="The name of a specific type of router.  Out of the box routers are 'default','column','bsh', and 'subselect.'  Custom routers can be configured as extension points." />
        <column name="router_expression" type="LONGVARCHAR"  description="An expression that is specific to the type of router that is configured in router_type.  See the documentation for each router for more details." />
        <column name="sync_on_update" type="BOOLEANINT" size="1" required="true" default="1"  description="Flag that indicates that this router should route updates." />
        <column name="sync_on_insert" type="BOOLEANINT" size="1" required="true" default="1"  description="Flag that indicates that this router should route inserts." />
        <column name="sync_on_delete" type="BOOLEANINT" size="1" required="true" default="1"  description="Flag that indicates that this router should route deletes." />
        <column name="create_time" type="TIMESTAMP" required="true"  description="Timestamp when this entry was created." />
        <column name="last_update_by" type="VARCHAR" size="50"  description="The user who last updated this entry." />
        <column name="last_update_time" type="TIMESTAMP" required="true"  description="Timestamp when a user last updated this entry." />
    </table>
	 */
	
	@Override
	public void writeInsertValues(DataWrite dp, Router obj)
			throws BasicException {
	
		dp.setString(1,obj.getRouterId());
		dp.setString(2,obj.getTargetCatalogName());
		dp.setString(3,obj.getTargetSchemaName());
		dp.setString(4,obj.getTargetTableName());
		dp.setString(5,obj.getNodeGroupLink().getSourceNodeGroupId());

		dp.setString(6,obj.getNodeGroupLink().getTargetNodeGroupId());
		dp.setString(7,obj.getRouterType());
		dp.setString(8,obj.getRouterExpression());
		dp.setBoolean(9,obj.isSyncOnUpdate());
		dp.setBoolean(10,obj.isSyncOnInsert());
		
		dp.setBoolean(11,obj.isSyncOnDelete());
		dp.setTimestamp(12, obj.getCreateTime());
		dp.setString(13,obj.getLastUpdateBy());
		dp.setTimestamp(14,obj.getLastUpdateTime());
	}

	@Override
	public Class getSuportClass() {
		return Router.class;
	}

}
